<template>
	<view class="content">
		<view class="card" v-for="(item,index) in dataList" :key="index">
			<view class="title-content column">
				<view style="display: flex;padding: 20rpx 20rpx 0 20rpx; justify-content: space-between;">
					<view class="title" style="font-size: 32rpx;font-weight: bold;">
						名称:
					</view>
					<view class="title-c" style="padding: 10rpx;border-radius: 8rpx;">
						{{item.name}}
					</view>
					<view class="title-r" @click="removeCoupon(item.id)">
						删除
					</view>
				</view>
				<view style="border-bottom: 1px solid #f6f6f6;margin: 20rpx;"></view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						类别
					</view>
					<view class="title-c">
						{{categoryFilter[item.category]}}
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						面值
					</view>
					<view class="title-c">
						{{item.money}}
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						领取方式
					</view>
					<view class="title-c">
						{{receiveTypeFilter[item.receiveType]}}
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						领取时间
					</view>
					<view class="title-c">
						<view v-if="item.receiveEndTime">{{ item.receiveStartTime }} - {{ item.receiveEndTime }}</view>
						<span v-else>不限时</span>
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						使用时间
					</view>
					<view class="title-c">
						<view v-if="item.day">{{ item.day }}天</view>
						<span v-else> {{ item.useStartTime }} - {{ item.useEndTime }} </span>
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						发布数量
					</view>
					<view class="title-c">
						<span v-if="!item.isLimited">不限量</span>
						<view v-else>
							<span class="fa">发布：{{ item.total }}</span>
							<span class="sheng">剩余：{{ item.lastTotal }}</span>
						</view>
					</view>
				</view>
				<view style="display: flex;padding: 20rpx;justify-content: space-between;">
					<view class="title">
						是否开启
					</view>
					<view class="title-c">
						<switch :checked="item.status" @change="switchChange(item)" class="switch-collen" />
					</view>
				</view>
			</view>
		</view>

        <!-- 确认弹窗组件 -->
		<event-dialog v-if="show" :message="messageText" @confirm="onConfirm" @cancel="onCancel"></event-dialog>

		<u-empty v-if="!dataList.length" style="background-color: #fff;height: 100vh;" mode="data"
			icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty>
		<!-- 上拉加载更多 -->
		<uni-load-more v-else :status="loadMoreStatus"></uni-load-more>
	</view>
</template>

<script>
	import {
		couponListdate,
		couponDeletedate,
		couponUpdateStatus
	} from '@/api/merchant.js';
	import {
		setThemeColor
	} from '../../../utils/setTheme';
	import eventDialog from './eventDialog.vue';
	let app = getApp();
	export default {
		components: {
			eventDialog
		},
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				cancellForm: {
					page: 1,
					limit: 10,
				},
				totalPage: 1,
				keyWord: '',
				loadMoreStatus: 'noMore',
				dataList: [],
				categoryFilter: ['-', '商家券', '商品券', '通用券', '品类券', '品牌券', '跨店券'],
				receiveTypeFilter: ['-', '手动领取', '商品赠送券', '平台活动发放'],
				messageText: '确认删除该优惠券吗？',
				show: false, //确认事件弹窗
				couponId: '',
			}
		},
		onLoad() {
			this.bgColor = setThemeColor()
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});
		},
		created() {
			this.initData();
		},
		methods: {
			initData() {
				// 首次激活时被调用
				this.cancellForm.page = 1;
				this.loadMoreStatus = 'more';
				// //需要初始化列表数据，根据数据类型
				// this.cancellForm.status = this.tab.type;
				this.getCouponList();
			},
			// 触底事件 onReachBottom 加载更多数据
			scrolltolowerFn() {
				if (this.loadMoreStatus != 'more') {
					return;
				};
				this.cancellForm.page++;
				this.getCouponList(true);
			},
			onrefresh(e) {
				this.refreshing = true;
				this.refreshText = "刷新中...";
				setTimeout(() => {
					this.refreshing = false;
					this.refreshFlag = false;
					this.refreshText = "已刷新";
				}, 2000)
			},
			onpullingdown(e) {
				if (this.refreshing) {
					return;
				}
				this.pulling = false;
				if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
					this.refreshFlag = true;
					this.refreshText = "释放立即刷新";
				} else {
					this.refreshFlag = false;
					this.refreshText = "下拉可以刷新";
				}
			},
			// 搜索查询
			searchProduct(valueKey) {
				if (valueKey == '') {
					delete this.cancellForm.name;
					this.cancellForm.current = 1;
					this.loadMoreStatus = 'more';
					this.getCouponList();
				} else {
					this.cancellForm.current = 1;
					this.cancellForm.name = valueKey;
					this.loadMoreStatus = 'more';
					this.getCouponList();
				}
			},
			switchChange: function(item) {
				couponUpdateStatus(item.id).then((res) => {
					uni.showToast({
						title: '状态修改成功！',
						icon: 'success', // 提示图标，可选值：'success', 'loading', 'none'
						duration: 2000 // 持续时间，单位为毫秒
					});
				})
			},
			// 开启状态筛选
			statusTypeConfirm(status) {
				this.cancellForm.current = 1;
				this.cancellForm.status = status;
				this.loadMoreStatus = 'more';
				this.getCouponList()
			},
			//领取类型状态筛选
			receiveTypeConfirm(receiveType) {
				this.cancellForm.current = 1;
				this.cancellForm.receiveType = receiveType;
				this.loadMoreStatus = 'more';
				this.getCouponList()
			},
			// 优惠券类别
			categoryeConfirm(category) {
				this.cancellForm.current = 1;
				this.cancellForm.category = category;
				this.loadMoreStatus = 'more';
				this.getCouponList()
			},
			// ----------------------
			getCouponList(isMerge) {
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (!isMerge) {
					this.cancellForm.page = 1;
					this.loadMoreStatus = 'more';
				};
				couponListdate(this.cancellForm).then((res) => {
					let data = res.data;
					if (isMerge) {
						that.$util.SplitArray(data.list, that.dataList);
					} else {
						that.dataList = data.list;
					}
					that.totalPage = data.totalPage;
					if (data.totalPage == this.cancellForm.page) {
						that.loadMoreStatus = 'noMore';
					}
					uni.hideLoading();
				})
			},

			//删除优惠券
			removeCoupon(id) {
				this.show = true;
				this.couponId = id;
			},
			onConfirm() {
				// 处理确认事件的逻辑
				this.show = false; // 关闭弹窗
				couponDeletedate(this.couponId).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: '删除成功！',
							icon: 'success', // 提示图标，可选值：'success', 'loading', 'none'
							duration: 2000 // 持续时间，单位为毫秒
						});
						setTimeout(() => {
							this.getCouponList();
						}, 1000)
					}
				});
			},
			onCancel() {
				this.show = false; // 关闭弹窗
			}
		}
	}
</script>

<style lang="scss">
	.bg_color {
		@include main_bg_color(theme);
	}

	.plain_bgColor {
		@include main_color(theme);
		@include main_bg_rgba_color(theme, 0.05);
		border: 1rpx solid;
	}

	.card {
		background-color: #fff;
		border-radius: 10rpx;
		margin: 20rpx;
	}

	.column {
		padding: 20rpx;
		display: flex;
		flex-flow: column;
		text-align: center;
		justify-content: space-between;

		.title {
			display: flex;
			// flex: 1;
			width: 150rpx;
		}

		.title-c {
			flex: 1;
			// justify-content: left;
			// padding-left: 20rpx;
			margin-left: 10rpx;
			display: flex;
			// justify-content: flex-end;
			font-size: 28rpx;
			font-weight: bold;
		}
	}
	.switch-collen{
		transform:scale(0.7)
	}
</style>